﻿<template>
    <el-dialog
        :title="titleMap[mode]"
        v-model="visible"
        :width="1000"
        destroy-on-close
        @closed="$emit('closed')"
    >
        <el-form
            :model="entityObj"
            :rules="rules"
            :disabled="mode == 'Show'"
            ref="dialogForm"
            label-width="100px"
        >
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="标题" prop="title">
                        <el-input v-model="entityObj.title" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="标签" prop="label">
                        <el-input v-model="entityObj.label" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="有效期" prop="dates">
                        <el-date-picker
                            style="width: 100%"
                            v-model="entityObj.dates"
                            type="datetimerange"
                            value-format="YYYY-MM-DD HH:mm:ss"
                            format="YYYY-MM-DD HH:mm:ss"
                            range-separator="-"
                            unlink-panels="true"
                            start-placeholder="开始时间"
                            end-placeholder="结束时间"
                            @change="changeProtocolDate"
                            :default-time="defaultTime"
                        />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="类型" prop="carouselImagesType">
                        <el-select placeholder="---请选择类型---" v-model="entityObj.carouselImagesType">
                            <el-option
                                v-for="item in optionsCarouselImagesType"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <!-- <el-col :span="12">
                    <el-form-item label-width="120px" label="用户类型" prop="orgType">
                        <el-select placeholder="---请选择类型---" v-model="entityObj.orgType">
                            <el-option
                                v-for="item in optionsType"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="是否首页显示" prop="status">
                        <el-switch
                            v-model="entityObj.isHome"
                            :active-value="1"
                            :inactive-value="0"
                        />
                    </el-form-item>
                </el-col> -->
            </el-row>
            <el-row>
                <el-col :span="12">
                <el-form-item label-width="120px" label="状态" prop="status">
                        <el-switch
                            v-model="entityObj.status"
                            :active-value="1"
                            :inactive-value="0"
                        />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label-width="120px" label="备注">
                        <el-input v-model="entityObj.remarks" type="textarea" :rows="2" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label-width="120px" label="图片" prop="imageUrl">
                        <sc-upload-multiple
                            v-model="entityObj.imageUrl"
                            :objName="'Supply/Carousel'"
                            draggable
                            :uploadType="'OSS'"
                            :bucketName="'jtmc'"
                            :limit="entityObj.carouselImagesType==1?2:1"
                            tip="最多上传1个文件,请上传图像格式文件"
                        />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label-width="120px" label="详情" prop="carouselDetail">
                        <sc-editor
                            ref="scCarouselDetail"
                            v-model="entityObj.carouselDetail"
                            :height="500"
                        ></sc-editor>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <el-button @click="visible = false">取 消</el-button>
            <el-button
                v-if="mode != 'Show'"
                type="primary"
                :loading="isSaveing"
                @click="submit()"
            >保 存</el-button>
        </template>
    </el-dialog>
</template>

<script>
import tool from "@/utils/tool";
export default {
    emits: ["success", "closed"],
    data() {
        return {
            mode: "Add",
            titleMap: {
                Add: "新增",
                Edit: "编辑",
                Show: "查看",
            },
            entityObj: {
                status: 1,
            },
            visible: false,
            isSaveing: false,
            needSaveFlag: false,
            optionsType: [],
            optionsCarouselImagesType: [],
            //验证规则
            rules: {
                title: [{ required: true, type: "string", message: "请填写" }],
                label: [{ required: true, type: "string", message: "请填写" }],
                orgType: [
                    { required: true, type: "number", message: "请填写" },
                ],
                carouselImagesType: [
                    { required: true, type: "number", message: "请填写" },
                ],
                status: [{ required: true, type: "number", message: "请填写" }],
                imageUrl: [
                    { required: true, type: "string", message: "请填写" },
                ],
                carouselDetail: [
                    { required: true, type: "string", message: "请填写" },
                ],
                isHome: [{ required: true, type: "number", message: "请填写" }],
                dates: [
                    { required: true, type: "array", message: "请填写" },
                ],
            },
            materialTypeTree: [],
            optionsBrand: [],
            optionsUnit: [],
            defaultTime:[
                new Date(2000, 1, 1, 0, 0, 0),
                new Date(2000, 2, 1, 23, 59, 59),
            ]
        };
    },
    methods: {
        //显示=
        open(mode = "Add") {
            this.mode = mode;
            this.visible = true;
            return this;
        },
        //表单提交方法
        submit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    this.isSaveing = true;
                    try {
                        var returnValue =
                            await this.$API.auth.PostProcessApi.post(
                                this.mode == "Add"
                                    ? "carousel-images/create"
                                    : "carousel-images/update",
                                { ...this.entityObj }
                            );
                        if (returnValue.status == 200) {
                            this.$message.success("操作成功");
                            this.$emit("fatherMethod");
                            this.isSaveing = false;
                            this.visible = false;
                        } else {
                            this.$message.error("操作失败:" + returnValue.msg);
                            this.isSaveing = false;
                        }
                    } catch (error) {
                        this.loading = false;
                        return false;
                    }
                }
            });
        },
        //表单注入数据
        async setData(data) {
            try {
                var returnValue = await this.$API.auth.GetProcessApi.get(
                    `carousel-images/${data.carouselImagesId}`,
                    {}
                );
                if (returnValue.status == 200) {
                    this.entityObj = returnValue.response;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        async initOptions() {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "base-dictionary/options",
                    { Codes: ["OrgType","CarouselImagesType"] }
                );
                if (returnValue.status == 200) {
                    this.optionsType = returnValue.response.find(
                        (p) => p.code == "OrgType"
                    ).optionsDetails;
                    this.optionsCarouselImagesType = returnValue.response.find(
                        (p) => p.code == "CarouselImagesType"
                    ).optionsDetails;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
    },
    async mounted() {
        this.entityObj.orgName = tool.data.get("USER_INFO").OrgName;
        await this.initOptions();
    },
};
</script>

<style></style>
